<?php
require_once ("model/User.php");
require_once ("Utils/Utils.php");

class LoginComponent {

	public function __construct($action) {

		$this->loginCheck();
		$this->logoffCheck();

		$isLoged = Utils::sessionGet("LOGGED_USER");
		
		if(!is_null($isLoged)) {
			$this->renderLogged($action, $isLoged);
		} else {
			$this->renderLogin($action);
		}
	
	}

	protected function renderLogged($action, $user) {

		$uid = $user->getUserId();
	
		$out = "<div align='center' style='border:1px solid black;width:130px;height:100px;padding-top:10px;'>
	   <span style='font-family:verdana;font-size:10px;'>Přihlášen</span><br>
	   <span style='font-family:verdana;font-size:10px;font-weight: bold;'>$uid</span><br>
	   <form action='$action' method='post'>
	      <input type='hidden' name='logoff' value='1'><br><br>
	      <input type='submit' value='Odhlásit' style='width:80px;font-family:verdana;font-size:10px;border:1px solid black;background-color:transparent;'>
	   </form>
		</div>";
		
		Utils::out($out);

	}

	protected function renderLogin($action) {

		$out = "<div align='center' style='border:1px solid black;width:130px;height:100px;padding-top:10px;'>
		  <form action='$action' method='post'>
		    <span style='font-family:verdana;font-size:10px;'>Přihl.jméno</span><br>
		    <input type='text' name='login' size='10' style='border:1px solid black;font-family:verdana;font-size:12px;text-align:center;'><br>
		    <span style='font-family:verdana;font-size:10px;'>Heslo</span><br>
		    <input type='password' name='password' size='10' style='border:1px solid black;font-family:verdana;font-size:12px;text-align:center;'><br><br>
		    <input type='submit' value='Prihlasit se' style='width:80px;font-family:verdana;font-size:10px;border:1px solid black;background-color:transparent;'>
		   </form>
		</div>";

		Utils :: out($out);

	}

	public function loginCheck() {
		$login = Utils :: requestPost('login');
		$password = Utils :: requestPost('password');

		if (!is_null($login) && !is_null($password)) {

			//NAHRADIT OPRAVDOVOU AUTENTIKACI Z DB

			if ($login == "azul" && $password == "azul") {
				$azul = new User();
				$azul->setUserId(12345);
				$azul->setCartNumber(555);
				
				Utils::sessionSet("LOGGED_USER",$azul);
			} else {
				Utils :: sendError("errorPage.php", "Nespravne jmeno a heslo");
			}

		}

	}
	
	public function logoffCheck(){
		$logof = Utils::requestPost('logoff');
		
		if(!is_null($logof) && $logof == 1){
			Utils::sessionRemove("LOGGED_USER");
		}
	}

}
?>
